package com.svs.framework.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
//basePackages = {"com.svs.system.mapper.master","com.svs.project.mapper.master"},
@Configuration
@MapperScan(basePackages = "com.svs.*.mapper.slave",sqlSessionFactoryRef = "slaveSqlSessionFactory")
public class MyBatisDbBConfig {
	
	 @Bean(name = "slaveDataSource")
	    @ConfigurationProperties(prefix = "spring.datasource.druid.slave")
	    public DataSource getDateSource2()
	    {
	        return DataSourceBuilder.create().build();
	    }
	 
	    @Bean(name = "slaveSqlSessionFactory")
	    public SqlSessionFactory test2SqlSessionFactory(@Qualifier("slaveDataSource") DataSource datasource)
	            throws Exception
	    {
	        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
	        bean.setDataSource(datasource);
	        bean.setTypeAliasesPackage("com.svs.*.domain");
	        bean.setMapperLocations(
	                new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/slave/*.xml"));
	        return bean.getObject();
	    }
	 
	    @Bean("slaveSqlSessionTemplate")
	    public SqlSessionTemplate test2SqlSessionTemplate(
	            @Qualifier("slaveSqlSessionFactory") SqlSessionFactory sessionFactory)
	    {
	        return new SqlSessionTemplate(sessionFactory);
	    }

	
}
